home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / MATH / RANDOMTP / TESTRAND.PAS < prev   
Pascal/Delphi Source File  |  1990-01-15  |  2KB  |  80 lines

  1. {$A+,B-,D-,E-,F-,I+,L-,N-,O-,R-,S-,V+}
  2. {$M 16384,0,655360}
  3. { This program verifies the performance of unit RAND.PAS }
  4. Program TestRand;
  5. Uses Rand, Crt;
  6.  
  7. Const
  8.   TicksPerDay = 1573040;
  9.   SecsPerDay = 24 * 60 * 60;
  10.   SecsPerTick = SecsPerDay / TicksPerDay;
  11.  
  12. Var
  13.   BiosClockTick : LongInt Absolute $40:$6C;
  14.   i, j, seed, Ticks : LongInt;
  15.   ch : Char;
  16.  
  17. Procedure ParkMiller;
  18. { This is the seed computation portion of the Park/Miller random number
  19.   generator.  See CACM Oct. '88. }
  20. Const
  21.   a = 16807;
  22.   m = 2147483647;
  23.   q = 127773;
  24.   r = 2836;
  25. Var
  26.   lo, hi, test : LongInt;
  27. Begin
  28.   hi := seed div q;
  29.   lo := seed mod q;
  30.   test := a * lo - r * hi;
  31.   If test > 0 Then
  32.     seed := test
  33.   Else
  34.     seed := test + m;
  35. End;
  36.  
  37. Begin
  38.   seed := 1;
  39.   RandSeed := 1;
  40.   Ticks := BiosClockTick;
  41.   For i := 1 To 10000 Do Begin
  42.     ParkMiller;
  43.     j := IRand;
  44.     If (i Mod 100000) = 0 Then { for timing };
  45.   End;
  46.   Ticks := BiosClockTick - Ticks;
  47.   WriteLn('10000 ', Seed, ' ', RandSeed);
  48.   { Both Seed and RandSeed should be 1043618065 (See CACM Oct. '88) }
  49.   If (seed = 1043618065) And (seed = RandSeed) Then Begin
  50.     WriteLn('Generators check OK at 10,000 iterations.');
  51.     WriteLn('You may now begin checking the full cycle, if you wish');
  52.     WriteLn('Warning: this could take a while - This cpu takes about ',
  53.        (SecsPerTick * Ticks * 10):4:1, ' sec per 100,000');
  54.     WriteLn('iterations.  The full cycle is ', RandModulo, ' iterations ',
  55.       '(about ', ((RandModulo/10000)*Ticks*SecsPerTick)/SecsPerDay:3:1,
  56.       ' cpu days).');
  57.     WriteLn('You may stop after any block of 100000 by pressing the ',
  58.       '<Escape> key.');
  59.     Write('Do you wish to begin checking the full cycle (Y/N)? ');
  60.     ch := UpCase(ReadKey);
  61.     WriteLn(ch);
  62.     If ch = 'Y' Then Begin
  63.       WriteLn('You asked for it!');
  64.       WriteLn('The two seed values will be displayed every 100000 iterations');
  65.       WriteLn('To terminate at next 100000, press <Escape>');
  66.       seed := 1;
  67.       RandSeed := 1;
  68.       For i := 1 To RandModulo Do Begin
  69.         ParkMiller;
  70.         j := IRand;
  71.         If (i Mod 100000) = 0 Then Begin
  72.           WriteLn(i, ' ', seed, ' ', RandSeed);
  73.           ch := ' ';
  74.           While KeyPressed Do ch := ReadKey;
  75.           If ch = ^[ Then Halt;
  76.         End;
  77.       End;
  78.     End;
  79.   End;
  80. End.